package com.bcmusic.distri.service.impl;

import com.bcmusic.distri.dao.DistributeDetailMapper;
import com.bcmusic.distri.pojo.DistributeDetail;
import com.bcmusic.distri.service.DistributeDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:DistributeDetail业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class DistributeDetailServiceImpl implements DistributeDetailService {

    @Autowired
    private DistributeDetailMapper distributeDetailMapper;


    /**
     * DistributeDetail条件+分页查询
     * @param distributeDetail 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<DistributeDetail> findPage(DistributeDetail distributeDetail, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(distributeDetail);
        //执行搜索
        return new PageInfo<DistributeDetail>(distributeDetailMapper.selectByExample(example));
    }

    /**
     * DistributeDetail分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<DistributeDetail> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<DistributeDetail>(distributeDetailMapper.selectAll());
    }

    /**
     * DistributeDetail条件查询
     * @param distributeDetail
     * @return
     */
    @Override
    public List<DistributeDetail> findList(DistributeDetail distributeDetail){
        //构建查询条件
        Example example = createExample(distributeDetail);
        //根据构建的条件查询数据
        return distributeDetailMapper.selectByExample(example);
    }


    /**
     * DistributeDetail构建查询对象
     * @param distributeDetail
     * @return
     */
    public Example createExample(DistributeDetail distributeDetail){
        Example example=new Example(DistributeDetail.class);
        Example.Criteria criteria = example.createCriteria();
        if(distributeDetail!=null){
            // 分发记录id。
            if(!StringUtils.isEmpty(distributeDetail.getDistributeDetailId())){
                    criteria.andEqualTo("distributeDetailId",distributeDetail.getDistributeDetailId());
            }
            // 音乐订单编号。关联order表的order_id字段。
            if(!StringUtils.isEmpty(distributeDetail.getOrderId())){
                    criteria.andEqualTo("orderId",distributeDetail.getOrderId());
            }
            // 分发商编号。关联distributor表的distributor_number字段。
            if(!StringUtils.isEmpty(distributeDetail.getDistributorNumber())){
                    criteria.andEqualTo("distributorNumber",distributeDetail.getDistributorNumber());
            }
            // 分发商获得的MB积分。即当被授权方通过分发商完成该订单时，分发商可获得的MB积分回馈。
            if(!StringUtils.isEmpty(distributeDetail.getOrderMbPoint())){
                    criteria.andEqualTo("orderMbPoint",distributeDetail.getOrderMbPoint());
            }
            //
            if(!StringUtils.isEmpty(distributeDetail.getCreateTime())){
                    criteria.andEqualTo("createTime",distributeDetail.getCreateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        distributeDetailMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改DistributeDetail
     * @param distributeDetail
     */
    @Override
    public void update(DistributeDetail distributeDetail){
        distributeDetailMapper.updateByPrimaryKey(distributeDetail);
    }

    /**
     * 增加DistributeDetail
     * @param distributeDetail
     */
    @Override
    public void add(DistributeDetail distributeDetail){
        distributeDetailMapper.insert(distributeDetail);
    }

    /**
     * 根据ID查询DistributeDetail
     * @param id
     * @return
     */
    @Override
    public DistributeDetail findById(Integer id){
        return  distributeDetailMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询DistributeDetail全部数据
     * @return
     */
    @Override
    public List<DistributeDetail> findAll() {
        return distributeDetailMapper.selectAll();
    }
}
